package gwt.bbsbot.client.widget;

import gwt.bbsbot.client.beans.QuestionDataBean;
import gwt.bbsbot.client.constatns.Constants;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;

public class TestBbsWidget extends BbsWidget {

	public TestBbsWidget() {
		MyGenre = Constants.Genre.TEST;
	}

	/**
	 * データの新規投稿イベントをボタンに追加する
	 * @param button
	 * @param name
	 * @param context
	 */
	protected void createAddHandler(Button button, String name, String context){
		button.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				//データ投稿
				greetingService.addQuestion(
						nameInDlg.getText(), 
						contentInDlg.getText(),
						MyGenre,
						new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								debug.setText(Constants.SERVER_ERROR);
							}

							@Override
							public void onSuccess(String result) {
								debug.setText(result);
								displayBBS();
							}			
						}//end of AsyncCallback
				);//end of addQuestion
				//dialogを隠す
				dlgInputForm.hide();
			}
		});
	}

	
	@Override
	public void displayBBS() {

//		greetingService.getAllQuestions(
		greetingService.getQuestionsByGenre(
				MyGenre,
				new AsyncCallback<List<QuestionDataBean>>(){

			@Override
			public void onFailure(Throwable caught) {
				debug.setText("getQuestions: " + Constants.SERVER_ERROR);				
			}

			@Override
			public void onSuccess(List<QuestionDataBean> result) {
				FlexTable tmpTable = new FlexTable();
				tmpTable.setBorderWidth(1);
				tmpTable.setText(0, 0, "ID");
				tmpTable.setText(0, 1, "投稿者");
				tmpTable.setText(0, 2, "内容");
				int index = 1;
				
				for(QuestionDataBean bean:result){
					
					//質問詳細リンク作成
					String host = Location.getHost();
					StringBuilder sb = new StringBuilder();
					sb.append("<a href=http://");
					sb.append(host);
					sb.append("/detail.html?id=");
					sb.append(bean.getId());
					sb.append(">");
					sb.append(bean.getId());
					sb.append("</a>");
					HTML html = new HTML();
					html.setHTML(sb.toString());
					tmpTable.setWidget(index, 0, html);
					
					//質問者のアイコンとか設定
					HTML name = new HTML();
					if(bean.getProfileImageURL() == null){
						name.setHTML(bean.getName());
					}else{
						name.setHTML("<img src=\"" + bean.getProfileImageURL() + "\" /><br>" + bean.getName());
					}
					tmpTable.setWidget(index, 1, name);
					
					tmpTable.setText(index, 2, bean.getQuestionData());
					index++;
					
					System.out.println(bean.toString());
				}
				if(panelFxBbs != null){
					panelFxBbs.remove(fxTable);
				}
				fxTable = tmpTable;
				panelFxBbs.add(fxTable);
			}
		});//end of getAllQuestions
		

	}

}
